/**
 * Created by CLAKE on 2015/10/16.
 */

var DIFFICULTY = 2;			//how fast the game gets mor difficult
var ROCK_TIME = 110;		//aprox tick count until a new asteroid gets introduced
var SUB_ROCK_COUNT = 4;		//how many small rocks to make on rock death
var BULLET_TIME = 5;		//ticks between bullets
var BULLET_ENTROPY = 100;	//how much energy a bullet has before it runs out.

var TURN_FACTOR = 7;		//how far the ship turns per frame
var BULLET_SPEED = 17;		//how fast the bullets move

var KEYCODE_ENTER = 13;		//useful keycode
var KEYCODE_SPACE = 32;		//useful keycode
var KEYCODE_UP = 38;		//useful keycode
var KEYCODE_LEFT = 37;		//useful keycode
var KEYCODE_RIGHT = 39;		//useful keycode
var KEYCODE_W = 87;			//useful keycode
var KEYCODE_A = 65;			//useful keycode
var KEYCODE_D = 68;			//useful keycod

/**
 * 游戏主体
 * @param id 画布ID
 * @constructor
 */
var GameDemo = function(id) {
    this.dom_id = id;
    //游戏暂停标记
    this.paused = false;
    //旋转主体
    this.main_body = null;
    //游戏舞台大小
    this.size = {width:0,height:0};
    //主圆中心坐标
    this.main_dot = null;
    //等待发射圆中心起始坐标
    this.ball_dot = null;
    //旋转速度
    this.speed = 3;
    //球移动速度
    this.ballSpeed = 20;
    //主体硬拼距离
    this.collide_length = 100;

    //连接旋转的小球
    this.rotate_list = [];
    //移动的小球
    this.ball = null;
    //是否已经发射小球
    this.move = false;
    //得分文字
    this.title = null;
    //FPS显示文字
    this.fps = null;
    //每次TICK执行时间
    this.time = 0;
    //关卡显示
    this.level_text = null;

    /********************************************************
     * 类方法
     ********************************************************/

    this.init = function() {
        this.stage = new createjs.Stage(this.dom_id);

        this.size.width = this.stage.canvas.width;
        this.size.height = this.stage.canvas.height;
        //主圆中心坐标
        this.main_dot = new createjs.Point(this.size.width/2,300);
        //创建主体圆
        this.main_body = new MainCircle({
            ang:0,
            dot:{x:this.main_dot.x,y:this.main_dot.y},
            radius:50
        });

        this.collide_length += 50;

        this.main_body.draw();
        //创建待发射球
        this.ball_dot = new createjs.Point(this.main_dot.x,this.main_dot.y+300);

        this.ball = this.createBall();
        this.ball.draw();
        //创建标题文字
        this.title = new createjs.Text();
        this.title.maxWidth = 200;
        this.title.x = this.main_dot.x;
        this.title.y = this.main_dot.y-280;
        this.title.font = 'bold 58px Arial';
        this.title.color = '#FFF';
        this.title.textAlign = 'center';
        this.title.text = this.rotate_list.length;

        //创建FPS显示文字
        this.fps = new createjs.Text();
        this.fps.maxWidth = 200;
        this.fps.x = 10;
        this.fps.y = this.size.height-30;
        this.fps.font = '24px Arial';
        this.fps.color = '#FFF';
        this.fps.text = '0';

        this.stage.addChild(this.main_body.body);
        this.stage.addChild(this.ball.body);
        this.stage.addChild(this.title);
        this.stage.addChild(this.fps);
    };

    /**
     * 创建一个球
     * @returns {Ball}
     */
    this.createBall = function() {
        return new Ball({
            ang:270,
            radius:15,
            dot:this.ball_dot,
            main_dot:this.main_dot
        });
    };

    /**
     * 游戏开始
     */
    this.start = function() {
        var self = this;
        this.init();
        createjs.Ticker.setFPS(60);
        createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
        //this.stage.update(event);
        createjs.Ticker.addEventListener("tick", function(event){
            self.tick(event);
        });
        window.addEventListener('keydown',function(e){
            if (e.keyCode == KEYCODE_SPACE) {
                self.move = true;
            }
        },false);
    };

    /**
     * 画主要圆形体
     */
    this.rotate = function() {
        for (var i in this.rotate_list) {
            this.rotate_list[i].setRadius(this.speed).draw();
        }
        this.main_body.addRadius(this.speed);
        this.main_body.draw();
    };

    /**
     * 移动发射中的球
     */
    this.moveBall = function() {
        if (this.move) {
            this.ball.move(this.ballSpeed);
            this.ball.draw();
        }
    };

    /**
     * 硬拼检测
     */
    this.checkCollide = function() {
        for (var i in this.rotate_list) {
            var curr_length = porint_length(this.rotate_list[i].dot,this.ball.dot);
            var collide_length = this.rotate_list[i].radius+this.ball.radius;
            if (curr_length < collide_length) {
                this.pause();
                alert('游戏结束');
                return;
            }
        }

        var line = porint_length(this.main_body.dot,this.ball.dot);
        if (line <= this.collide_length) {
            console.log(line);
            //检查到球到达硬拼边沿就把球放入旋转队例
            this.ball.setPorint(get_length_porint(this.collide_length,90,this.main_dot.x,this.main_dot.y));
            this.ball.draw();
            this.rotate_list.push(this.ball);
            this.ball.link();
            //新创建一个球
            this.ball = this.createBall();
            this.ball.draw();
            this.stage.addChild(this.ball.body);
            this.move = false;
            //更新文字信息
            this.title.text = this.rotate_list.length;
        }
    };

    this.updateFps = function(time) {
        var diff = time-this.time;
        this.time = time;
        this.fps.text = 'FPS:'+Math.round(1000/diff);
    };

    /**
     * 每帧方法
     */
    this.tick = function(event) {
        if (this.paused) {
            return;
        }
        this.rotate();
        this.moveBall();
        this.checkCollide();
        this.updateFps(event.time);
        this.stage.update(event);
    };

    /**
     * 暂停或继续游戏
     */
    this.pause = function() {
        this.paused = !this.paused;
    };
};

var MainCircle = function(opt) {
    /********************************************************
     * private property 私有属性
     ********************************************************/
    //全局类指向
    var self = this;
    /********************************************************
     * public property 公有属性
     ********************************************************/
    //圆点中心坐标
    this.dot = null;
    //弧度点坐标
    this.ang_dot = null;
    //主圆半径
    this.radius = 100;
    //初始角度
    this.ang = 0;
    //填充颜色
    this.fill_color = 'DeepSkyBlue';
    /**
     * 主体画布
     * @type createjs.Shape
     */
    this.body = new createjs.Shape();
    /********************************************************
     * private function 私有方法
     ********************************************************/
    function init() {
        self.radius = opt.radius || 100;
        self.ang = opt.ang || 0;
        self.dot = opt.dot || {x:0,y:0};
        self.ang_dot = get_length_porint(self.radius,self.ang,self.dot.x,self.dot.y);
        self.fill_color = opt.fill_color || 'DeepSkyBlue';
    }
    /********************************************************
     * public function 公有方法
     ********************************************************/
    this.draw = function(){
        this.body.graphics.clear();
        this.body.graphics.beginFill(this.fill_color).drawCircle(this.dot.x,this.dot.y,this.radius).endFill();
        this.body.graphics.beginStroke('#FFFFFF')
            .moveTo(this.dot.x,this.dot.y)
            .lineTo(this.ang_dot.x,this.ang_dot.y)
            .endStroke();
    };

    this.addRadius = function(ang) {
        this.ang += ang;
        if (this.ang > 360) {
            this.ang -= 360;
        }
        this.ang_dot = get_length_porint(self.radius,self.ang,self.dot.x,self.dot.y);
    };

    /********************************************************
     * construct 构造方法
     ********************************************************/
    init();
};

var Ball = function(opt) {
    /********************************************************
     * private property 私有属性
     ********************************************************/
    //全局类指向
    var self = this;
    /********************************************************
     * public property 公有属性
     ********************************************************/
    //圆点中心坐标
    this.dot = null;
    //弧度点坐标
    this.main_dot = null;
    //主圆半径
    this.radius = 100;
    //初始角度
    this.ang = 0;
    //填充颜色
    this.fill_color = 'DeepSkyBlue';
    //是否连接
    this.linked = false;
    /**
     * 主体画布
     * @type createjs.Shape
     */
    this.body = new createjs.Shape();
    /********************************************************
     * private function 私有方法
     ********************************************************/
    function init() {
        self.radius = opt.radius || 100;
        self.ang = opt.ang || 0;
        self.start_ang = opt.start_ang || 90;
        self.dot = opt.dot || {x:0,y:0};
        self.main_dot = opt.main_dot || {x:0,y:0};
        self.fill_color = opt.fill_color || 'DeepSkyBlue';
    }

    /********************************************************
     * public function 公有方法
     ********************************************************/
    this.draw = function(){
        this.body.graphics.clear();
        if (this.linked) {
            this.body.graphics.beginStroke('#FFFFFF')
                .moveTo(this.dot.x,this.dot.y)
                .lineTo(this.main_dot.x,this.main_dot.y)
                .endStroke();
        }
        this.body.graphics.beginFill(this.fill_color).drawCircle(this.dot.x,this.dot.y,this.radius).endFill();

    };

    this.setRadius = function(ang) {
        this.start_ang += ang;
        if (this.start_ang > 360) {
            this.start_ang -= 360;
        }
        var length = porint_length(this.dot,this.main_dot);
        this.dot = get_length_porint(length,this.start_ang,this.main_dot.x,this.main_dot.y);

        return this;
    };

    this.setPorint = function(dot) {
        this.dot = dot;
        return this;
    };

    this.move = function(length) {
        this.dot = get_length_porint(length,this.ang,this.dot.x,this.dot.y);
        return this;
    };

    this.link = function() {
        this.linked = true;
        return this;
    };

    /********************************************************
     * construct 构造方法
     ********************************************************/
    init();
};